package com.cheng.mybatis.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.SystemPropertyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cheng.mybatis.domain.Book;
import com.cheng.mybatis.domain.Bookdetailed;
import com.cheng.mybatis.service.BookService;

@Controller
public class BookController {

	@Autowired
	private BookService bookservice;

	@RequestMapping("/bookListServlet")
	public String ShowAll(HttpServletRequest request) {

		// 获取到所有书的信息 并将其放入 session 域中 然后跳转到 list 这个
		// jsp 并取出来
		List<Book> lists = bookservice.showAll();

		if (lists.size() > 0) {

			request.setAttribute("books", lists);
		}

		return "admin/products/list.jsp";

	}

	// 根据条件去 查询
	@RequestMapping("/searchBooksServlet")
	public String searchBooksServlet(String bname, HttpServletRequest request) {

		System.out.println(bname);
		List<Book> lists = bookservice.serachByName(bname);

		if (lists.size() > 0) {

			request.setAttribute("books", lists);
		}

		return "admin/products/list.jsp";

	}

	// addBookServlet 添加书籍
	@RequestMapping("/addBook")
	public String addBook(Book book, Bookdetailed bookdetailed, HttpServletRequest request) {

		System.out.println("Sssssssss");
		System.out.println(bookdetailed);
		System.out.println(book);

		List<Book> lists = bookservice.addBook(book, bookdetailed);

		if (lists.size() > 0) {

			request.setAttribute("books", lists);
		}

		return "admin/products/list.jsp";

	}

	/* deleteBook.action */
	@RequestMapping("/deleteBook")
	public String deleteBookBuNumber(String number, HttpServletRequest request) {

		List<Book> lists = bookservice.deleteBook(number);

		if (lists.size() > 0) {

			request.setAttribute("books", lists);
		}

		return "admin/products/list.jsp";

	}

	// findBookByNumber.action
	@RequestMapping("/findBookByNumber")
	public String findBookByNumber(String number, HttpServletRequest request) {
		Book book = bookservice.serachByNumber(number);
		Bookdetailed bookdetailed = bookservice.serachByNumber2(number);
		if (book != null) {
			request.setAttribute("book", book);
		}
		if (bookdetailed != null) {
			request.setAttribute("bookdetailed", bookdetailed);
		}
		return "admin/products/edit.jsp";
	}

	@RequestMapping("/updateBook")
	public String updateBook(Book book, HttpServletRequest request) {

		List<Book> lists = bookservice.updateBook(book);

		if (lists.size() > 0) {

			request.setAttribute("books", lists);
		}

		return "admin/products/list.jsp";

	}

}
